﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Core;
using eBlog.Entity;

namespace eBlog.Web.Admin
{
    public partial class LinksManage : PageBase
    {
      
        readonly IDBBase dal = DBFactory.GetDbProvider(); //初始化数据工厂

		protected void Page_Load(object sender, EventArgs e)
		{


            PageValidate.CheckAdminLogin(this);

            if (!Page.IsPostBack)
            {
                switch (action)
                {
                    case "add":
                    case "edit":
                        ShowInfo();
                        BindList();
                        break;
                    case "del":
                        delInfo();
                        break;
					case "show":
                        break;
                    default:
                        BindList();
                        break;
                }
            }
			else
            {
                if (action == "del")
                    delInfo();
            }

		}

		private void BindList()
        {
            int PageIndex;
            if (!int.TryParse(Request.Params["page"], out PageIndex)) { PageIndex = 1; }

            ArrayList attrs = dal.Page(PageIndex, PageSize).GetListAndCount<Links>();

            List<Links> lists = attrs[0] as List<Links>;
            ListCount = (int)attrs[1];
            RPLIST.DataSource = lists;
            RPLIST.DataBind();
            PageNav.Text = Pager.PageInfo(ListCount, PageSize, PageIndex); 
        }

					
		public void btnSave_Click(object sender, EventArgs e)
		{
			ArrayList strErrs = new ArrayList(); //Add时候必须用::分开控件名
			this.edit.Visible = true;
			
			if(this.txtName.Text =="")
			{
				strErrs.Add("txtName::Name不能为空，请输入字符！"); 
			}
			if(this.txtHref.Text =="")
			{
				strErrs.Add("txtHref::Href不能为空，请输入字符！"); 
			}
			
			
			if(this.txtDescription.Text =="")
			{
				strErrs.Add("txtDescription::Description不能为空，请输入字符！"); 
			}
			if(!PageValidate.IsNumber(txtDisplayorder.Text))
			{
				strErrs.Add("txtDisplayorder::Displayorder必须输入数字整数！"); 
			}
			
			
			
			if (strErrs.Count > 0) 
			{
				string[] arrayErrs = (string[])strErrs.ToArray(typeof(string));
				string strErr = string.Join("\\n", arrayErrs); 
				//MessageBox.Show(this,strErr);
				ErrCodeStr.Value = strErr;
				return;
			}

			//int Type=int.Parse(this.txtType.Text);
			string Name=this.txtName.Text;
            string Href = this.txtHref.Text.Replace("${siteurl}", ConfigHelper.SiteUrl);
			//int Position=int.Parse(this.txtPosition.Text);
			//string Target=this.txtTarget.Text;
			string Description=this.txtDescription.Text;
			int Displayorder=int.Parse(this.txtDisplayorder.Text);
			//int Status=int.Parse(this.txtStatus.Text);
			//DateTime Createdate=DateTime.Parse(this.txtCreatedate.Text);
			//int Hit=int.Parse(this.txtHit.Text);


			if (action == "edit" && id > 0)
			{
				Links model = dal.GetModel<Links>(id); //为了不覆盖更新,先读出存在的数据再按需填充数据

			
				model.Name=Name;
				model.Href=Href;
				model.Description=Description;
				model.Displayorder=Displayorder;
                model.Status = chkStatus.Checked ? 1 : 0;
                model.Position = chkPosition.Checked ? (int)LinkPosition.Navigation : (int)LinkPosition.General;
                model.Target = chkTarget.Checked ? "_blank" : "_self";


				dal.Update<Links>(model);
				MessageBox.ShowAndRedirect(this, "修 改 完 成!", Referrer.Value);
				return;
			}
			else
			{
				Links model = new Links();  //添加数据,准备新的实体

			    model.Type = 0;
				model.Name=Name;
				model.Href=Href;
				model.Description=Description;
				model.Displayorder=Displayorder;
			    model.Status = 0;
				model.Createdate=DateTime.Now;
			    model.Hit = 0;
                model.Status = chkStatus.Checked ? 1 : 0;
                model.Position = chkPosition.Checked ? (int)LinkPosition.Navigation : (int)LinkPosition.General;
                model.Target = chkTarget.Checked ? "_blank" : "_self";

				Response.Redirect(Request.Url.AbsolutePath + "#" + dal.Add<Links>(model)); //ADD并 返回当前Uri绝对地址不带参数
				return;
			}
			
		}

		private void ShowInfo()
		{
			try { this.Referrer.Value = Request.UrlReferrer.PathAndQuery; }  catch { }
			if (id > 0 && action == "edit")
			{
				Links model = dal.GetModel<Links>(id);
				txtName.Text=model.Name;
				txtHref.Text=model.Href;
				txtDescription.Text=model.Description;
				txtDisplayorder.Text=model.Displayorder.ToString();
                chkStatus.Checked = model.Status == 1 ? true : false;
                chkPosition.Checked = model.Position == (int)LinkPosition.Navigation ? true : false;
                chkTarget.Checked = model.Target == "_blank" ? true : false;
				btnSave.Text = "编 辑";
			}

		}




		private void delInfo()
		{
			string ids = PageValidate.Form("checkallid");
            if (!String.IsNullOrEmpty(ids))
            {
                //dal.From("Links", "").Where("id in (@ids)").SetString("ids", ids).Delete();
                dal.From("Links", "").Where("id in ("+ids+")").Delete();
            }
            else
			{
				dal.Delete<Links>(id);
			}

			Response.Redirect(Request.Url.AbsolutePath); //返回当前Uri绝对地址不带参数
			return;
		}


    }
}

